home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / fchart / fmisc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  13.2 KB  |  560 lines

  1. /* Fchart - fmisc.c */
  2. /*
  3.  * Gnuplot code
  4.  * Copyright (C) 1986, 1987, 1990   Thomas Williams, Colin Kelley
  5.  *
  6.  * Permission to use, copy, and distribute this software and its
  7.  * documentation for any purpose with or without fee is hereby granted,
  8.  * provided that the above copyright notice appear in all copies and
  9.  * that both that copyright notice and this permission notice appear
  10.  * in supporting documentation.
  11.  *
  12.  * Permission to modify the software is granted, but not the right to
  13.  * distribute the modified code.  Modifications are to be distributed
  14.  * as patches to released version.
  15.  *
  16.  * This software  is provided "as is" without express or implied warranty.
  17.  *
  18.  *
  19.  * AUTHORS
  20.  *
  21.  *   Original Software:
  22.  *     Thomas Williams,  Colin Kelley.
  23.  *
  24.  *   Gnuplot 2.0 additions:
  25.  *       Russell Lang, Dave Kotz, John Campbell.
  26.  *
  27.  *   Fchart changes and additions:
  28.  *       Piotr Filip Sawicki
  29.  *
  30.  * send your comments or suggestions to fs@uwasa.fi
  31.  *
  32.  */
  33.  
  34. #include <stdio.h>
  35. #include "plot.h"
  36. #include "fchart.h"
  37. #include "help.h"
  38.  
  39. #ifdef __TURBOC__
  40. #include <graphics.h>
  41. #endif
  42.  
  43. extern BOOLEAN autoscale;
  44. extern BOOLEAN auto_label;
  45. extern BOOLEAN log_y;
  46. extern BOOLEAN b_clockwise, p_clockwise;
  47. extern BOOLEAN draw_border;
  48. extern FILE* outfile;
  49. extern char outstr[];
  50. extern int samples;
  51. extern int term;
  52. extern double zero;
  53. extern double base;
  54. extern double radexp;
  55. extern double b_wid, b_spc, b_int;
  56. extern float xsize, ysize;
  57. extern double roff, loff, toff, boff;
  58. extern enum GRAV_DIR gravity, explode;
  59. extern enum INP_STYLE inp_style;
  60. extern enum DRAW_STYLE data_style;
  61. extern enum FONT_STYLE vect_font;
  62. extern struct pair data_place, label_plac;
  63. extern int HLitem;
  64. extern struct dfile data_head;
  65. extern int xmin, xmax;
  66. extern double treshold;
  67. extern char thrname[];
  68. extern char tic_form[];
  69. extern int strunc;
  70.  
  71. extern BOOLEAN screen_ok;
  72.  
  73. extern int c_token, num_tokens;
  74. extern struct termentry term_tbl[];
  75.  
  76. extern char *sprintf(),*strcpy();
  77. static char *grav_name();
  78.  
  79. extern void show_labels(), show_arrow();
  80.  
  81. char *alloc(size, message)
  82. unsigned size;
  83. char *message;
  84. {
  85.     char *malloc();
  86.     char *p;                /* the new allocation */
  87.     char errbuf[100];       /* error message string */
  88.     extern char *malloc();
  89.  
  90.     p = malloc(size);
  91.     if (p == (char *)NULL) {
  92.  
  93. #ifndef VMS
  94.        FreeHelp();          /* out of memory, try to make some room */
  95. #endif
  96.  
  97.        p = malloc(size);        /* try again */
  98.        if (p == (char *)NULL) {
  99.           /* really out of memory */
  100.           if (message != NULL) {
  101.              (void) sprintf(errbuf, "out of memory for %s", message);
  102.              int_error(errbuf, NO_CARET);
  103.              /* NOTREACHED */
  104.           }
  105.           /* else we return NULL */
  106.        }
  107.     }
  108.  
  109.     return(p);
  110. }
  111.     
  112. destroy(p)        /* destroy dfile structure p and all the following */
  113. struct dfile *p;
  114. {
  115. struct dfile *t;
  116. struct chunk *a, *b;
  117. int i;
  118.  
  119.     while (p) {
  120.         for (a=p->data; a; ) {
  121.             free((char *)a->dval);
  122.             if (a->vlbl) {
  123.                 for (i=0; i<a->used; i++)
  124.                     if (a->vlbl[i])
  125.                         free(a->vlbl[i]);
  126.                 free((char *)a->vlbl);
  127.             }
  128.             b = a;
  129.             a = a->next;
  130.             free((char *)b);
  131.         }
  132.         if (p->fname) free(p->fname);
  133.         t = p;
  134.         p = p->dnxt;
  135.         free((char *)t);
  136.     }
  137. }
  138.  
  139. load_file(fp)
  140. FILE *fp;
  141. {
  142.     register int len;
  143.     extern char input_line[];
  144.     int start, left, more, stop = FALSE;
  145.  
  146.     if (!fp)
  147.         os_error("Cannot open load file",c_token);
  148.     
  149.     while (!stop) {      /* read all commands in file */
  150.         /* read one command */
  151.         left = MAX_LINE_LEN;
  152.         start = 0;
  153.         more = TRUE;
  154.         
  155.         while (more) {
  156.             if (fgets(&(input_line[start]), left, fp) == NULL) {
  157.                 stop = TRUE; /* EOF in file */
  158.                 input_line[start] = '\0';
  159.                 more = FALSE;
  160.             } else {
  161.                 len = strlen(input_line) - 1;
  162.                 if (input_line[len] == '\n') { /* remove any newline */
  163.                     input_line[len] = '\0';
  164.                     /* Look, len was 1-1 = 0 before, take care here! */
  165.                     if (len > 0) --len;
  166.                 } else if (len+1 >= left)
  167.                     int_error("Input line too long",NO_CARET);
  168.                 
  169.                 if (input_line[len] == '\\') { /* line continuation */
  170.                     start = len;
  171.                     left -= len;
  172.                 } else
  173.                     more = FALSE;
  174.             }
  175.         }
  176.         
  177.         if (strlen(input_line) > 0) {
  178.             screen_ok = FALSE; /* make sure command line is
  179.                                   echoed on error */
  180.             do_line();
  181.         }
  182.     }
  183. }
  184.  
  185. save_sets(fp)        /* save all current settings */
  186. FILE *fp;
  187. {
  188.     char comm[MAX_LINE_LEN+1];
  189.     
  190.     if (!fp)
  191.         os_error("Cannot open save file",c_token);
  192.  
  193.     for(c_token++; !END_OF_COMMAND; c_token++) {
  194.         if (equals(c_token,",")) continue;
  195.         if (!isstring(c_token)) break;        /* causes error, but later */
  196.         quote_str(comm, c_token);
  197.         fprintf(fp,"# %s\n",comm);
  198.     }
  199.  
  200.     (void) putc('\n',fp);
  201.     fprintf(fp,"set %sautolabel\n", auto_label ? "" : "no");
  202.     fprintf(fp,"set %struncate", strunc==-1 ? "no" : "");
  203.     if (strunc>0)
  204.         fprintf(fp," %d", strunc);
  205.     (void) putc('\n',fp);
  206.     fprintf(fp,"set cust ");
  207.     if (data_place.from != -1) {
  208.         show_segment(data_place.from,data_place.upto,fp);
  209.         (void) putc(' ',fp);
  210.         if (label_plac.from != -1)
  211.             show_segment(label_plac.from,label_plac.upto,fp);
  212.     }
  213.     (void) putc('\n',fp);
  214.     fprintf(fp,"set font ");
  215.     switch (vect_font) {
  216.         case F_NEVER: {
  217.             fprintf(fp,"never\n");
  218.             break;
  219.         }
  220.         case F_WHENN: {
  221.             fprintf(fp,"when_needed\n");
  222.             break;
  223.         }
  224.         case F_ROTAT: {
  225.             fprintf(fp,"rotated\n");
  226.             break;
  227.         }
  228.         case F_ALWYS: {
  229.             fprintf(fp,"always\n");
  230.             break;
  231.         }
  232.     }
  233.     fprintf(fp,"set format \"%s\"\n", tic_form);
  234.     fprintf(fp,"set %sframe\n", draw_border ? "" : "no");
  235.     fprintf(fp,"set highlight ");
  236.     switch (HLitem) {
  237.         case HL_MIN: {
  238.             fprintf(fp,"min\n");
  239.             break;
  240.         }
  241.         case HL_MAX: {
  242.             fprintf(fp,"max\n");
  243.             break;
  244.         }
  245.         case HL_NON: {
  246.             fprintf(fp,"none\n");
  247.             break;
  248.         }
  249.         default: {
  250.             fprintf(fp,"%d\n",HLitem);
  251.             break;
  252.         }
  253.     }
  254.     fprintf(fp,"set input ");
  255.     switch (inp_style) {
  256.         case GNUPLOT: {
  257.             fprintf(fp,"gnuplot\n");
  258.             break;
  259.         }
  260.         case PRIVATE: {
  261.             fprintf(fp,"private\n");
  262.             break;
  263.         }
  264.         case CUSTOMD: {
  265.             fprintf(fp,"customized\n");
  266.             break;
  267.         }
  268.     }
  269.     fprintf(fp,"set %slogscale\n", log_y ? "" : "no");
  270.     fprintf(fp,"set offsets %lg, %lg, %lg, %lg\n", loff, roff, toff, boff);
  271.     fprintf(fp,"set output %s\n", strcmp(outstr,"STDOUT") ? outstr : "");
  272.     fprintf(fp,"set size %g, %g\n", xsize, ysize);
  273.     fprintf(fp,"set style ");
  274.     switch (data_style) {
  275.         case ABARS:        fprintf(fp,"adj\n"); break;
  276.         case SBAR:        fprintf(fp,"sta\n"); break;
  277.         case LAYB:        fprintf(fp,"lay\n"); break;
  278.         case PIECHART:    fprintf(fp,"pie\n"); break;
  279.     }
  280.     fprintf(fp,"set term %s\n", term_tbl[term].name);
  281.     fprintf(fp,"set title ");
  282.     if (data_head.fname) fprintf(fp,"\"%s\"\n", data_head.fname);
  283.     else (void) putc('\n',fp);
  284.     show_labels(0, fp, TRUE);
  285.     show_arrow(0, fp, TRUE);
  286.     fprintf(fp,"set range ");
  287.     if (xmin != -1) show_segment(xmin,xmax,fp);
  288.     (void) putc('\n',fp);
  289.     fprintf(fp,"set zero %lg\n", zero);
  290.  
  291.     (void) putc('\n',fp);
  292.     
  293.     fprintf(fp,"set bar %sautoscale\n", autoscale ? "" : "no");
  294.     fprintf(fp,"set bar base %lg\n", base);
  295.     fprintf(fp,"set bar %sclock\n", b_clockwise ? "" : "counter_");
  296.     fprintf(fp,"set bar grav %s\n", grav_name(gravity));
  297.     fprintf(fp,"set bar width %lg, %lg, %lg\n", b_wid, b_int, b_spc);
  298.  
  299.     (void) putc('\n',fp);
  300.  
  301.     fprintf(fp,"set pie expl %s\n", grav_name(explode));
  302.     fprintf(fp,"set pie %sclockwise\n", p_clockwise ? "" : "counter_");
  303.     fprintf(fp,"set pie radius %lg\n", radexp);
  304.     fprintf(fp,"set pie samples %d\n", samples);
  305.     fprintf(fp,"set pie threshold %lg \"%s\"\n", treshold, thrname);
  306.  
  307.     (void) fclose(fp);
  308. }
  309.  
  310. int instring(str, c)
  311. char *str;
  312. char c;
  313. {
  314.     int pos = 0;
  315.     
  316.     while (str != NULL && *str != '\0' && c != *str) {
  317.         str++;
  318.         pos++;
  319.     }
  320.     return (pos);
  321. }
  322.  
  323. show_style()
  324. {
  325.     fprintf(stderr,"\tdata are plotted with ");
  326.     switch (data_style) {
  327.         case ABARS: fprintf(stderr,"adjacent bars\n"); break;
  328.         case LAYB: fprintf(stderr,"layer bars\n"); break;
  329.         case SBAR: fprintf(stderr,"stacked bars\n"); break;
  330.         case PIECHART: fprintf(stderr,"piechart\n"); break;
  331.     }
  332. }
  333.  
  334. show_range()
  335. {
  336.     if (xmin==-1)
  337.         fprintf(stderr,"\twhole file is processed");
  338.     else {
  339.         fprintf(stderr,"\tdata range is: ");
  340.         show_segment(xmin,xmax,stderr);
  341.     }
  342.     (void) putc('\n',stderr);
  343. }
  344.  
  345. show_zero()
  346. {
  347.     fprintf(stderr,"\tzero/epsilon is %lg\n",zero);
  348. }
  349.  
  350. show_offsets()
  351. {
  352.     fprintf(stderr,"\toffsets are %lg, %lg, %lg, %lg\n",loff,roff,toff,boff);
  353. }
  354.  
  355. show_samples()
  356. {
  357.     fprintf(stderr,"\tsampling rate for piechart is %d\n",samples);
  358. }
  359.  
  360. show_output()
  361. {
  362.     fprintf(stderr,"\toutput is sent to %s\n",outstr);
  363. }
  364.  
  365. show_term()
  366. {
  367.     fprintf(stderr,"\tterminal type is %s\n",term_tbl[term].name);
  368. }
  369.  
  370. show_autoscale()
  371. {
  372.     fprintf(stderr,"\tbar autoscaling is %s\n",(autoscale)? "ON" : "OFF");
  373. }
  374.  
  375. show_logscale()
  376. {
  377.     if (log_y)
  378.         fprintf(stderr,"\tlogscaling is ON\n");
  379.     else
  380.         fprintf(stderr,"\tno logscaling\n");
  381. }
  382.  
  383. show_version()
  384. {
  385. extern char version[];
  386. extern char date[];
  387. static char *authors[] = {"Thomas Williams","Colin Kelley"};
  388. extern int patchlevel;
  389. extern char bug_email[];
  390. int x;
  391. long time();
  392.  
  393.     x = time((long *)NULL) & 1;
  394.     fprintf(stderr,"\n\t%s\n\t%sversion %s patchlevel %d\n\tlast modified %s\n",
  395.         PROGRAM, OS, version, patchlevel, date);
  396.     fprintf(stderr,"\tGnuplot v.1.x Copyright (C) 1986, 1987  %s, %s\n",
  397.         authors[x],authors[1-x]);
  398.     fprintf(stderr,"\tGnuplot v.2.0 John Campbell, David Kotz, Russell Lang\n");
  399.     fprintf(stderr,"\t%s Copyright (C) 1990 Piotr Filip Sawicki\n",PROGRAM);
  400. #ifdef __TURBOC__
  401.     fprintf(stderr,"\tCreated using Turbo C, Copyright Borland 1987, 1988\n");
  402. #endif
  403.     fprintf(stderr, "\n\tSend bugs and comments to %s\n\n", bug_email);
  404. }
  405.  
  406. static char *grav_name(what)
  407. enum GRAV_DIR what;
  408. {
  409.     static char buf[20];
  410.     switch (what) {
  411.         case(SOUTH)   : (void) strcpy(buf,"bottom"); break;
  412.         case(NORTH)   : (void) strcpy(buf,"top"); break;
  413.         case(EAST)    : (void) strcpy(buf,"right"); break;
  414.         case(WEST)    : (void) strcpy(buf,"left"); break;
  415.         case(DEFAULT) : buf[0] = '\0'; break;
  416.     }
  417.     return(buf);
  418. }
  419.  
  420. show_gravity()
  421. {
  422.     fprintf(stderr,"\tbar gravitation to the %s\n", grav_name(gravity));
  423. }
  424.  
  425. show_explode()
  426. {
  427.     fprintf(stderr,"\thighlited slice explodes %s\n",
  428.             explode == DEFAULT
  429.                 ? "where it should"
  430.                 : grav_name(explode));
  431. }
  432.  
  433. show_HLset()
  434. {
  435.     switch (HLitem) {
  436.         case(HL_NON): fprintf(stderr,"\tno item"); break;
  437.         case(HL_MIN): fprintf(stderr,"\tminimal item"); break;
  438.         case(HL_MAX): fprintf(stderr,"\tmaximal item"); break;
  439.         default     : fprintf(stderr,"\titem no. %d",HLitem); break;
  440.     }
  441.     fprintf(stderr," is highlited\n");
  442. }
  443.  
  444. show_base()
  445. {
  446.     fprintf(stderr,"\tbase for bars is %lg\n",base);
  447. }    
  448.  
  449. show_input()
  450. {
  451.     switch (inp_style) {
  452.         case(GNUPLOT) : fprintf(stderr,"\tgnuplot-compatible"); break;
  453.         case(PRIVATE) : fprintf(stderr,"\tstandard"); break;
  454.         case(CUSTOMD) : fprintf(stderr,"\tcustomized"); break;
  455.     }
  456.     fprintf(stderr," data input style\n");
  457. }
  458.  
  459. show_custom()
  460. {
  461.     if (data_place.from == -1) 
  462.         fprintf(stderr,"\tcustomized input format not given\n");
  463.     else {
  464.         fprintf(stderr,"\tcustomized input format:\n");
  465.         fprintf(stderr,"\t\tvalue: ");
  466.         show_segment(data_place.from,data_place.upto,stderr);
  467.         if (label_plac.from == -1) fprintf(stderr,"\n\t\tno label position");
  468.         else {
  469.             fprintf(stderr,"\n\t\tlabel: ");
  470.             show_segment(label_plac.from,label_plac.upto,stderr);
  471.         }
  472.         (void) putc('\n',stderr);
  473.     }
  474. }
  475.  
  476. static show_segment(l,r,fp)
  477. int l,r;
  478. FILE *fp;
  479. {
  480.     (void) putc('[',fp);
  481.     if (l) fprintf(fp,"%d",l);
  482.     fprintf(fp," : ");
  483.     if (r>-1) fprintf(fp,"%d",r);
  484.     (void) putc(']',fp);
  485. }
  486.  
  487. show_label()
  488. {
  489.     fprintf(stderr,"\tlabels are%s auto-generated\n",
  490.             auto_label ? "" : "n't");
  491. }
  492.  
  493. show_clock(pie)
  494. BOOLEAN pie;
  495. {
  496.     if (pie)
  497.         fprintf(stderr,"\tpies are drawn %sclockwise\n",
  498.                 p_clockwise ? "" : "counter-");
  499.     else
  500.         fprintf(stderr,"\tbars are drawn %sclockwise\n",
  501.                 b_clockwise ? "" : "counter-");
  502. }
  503.  
  504. show_width()
  505. {
  506.     fprintf(stderr,"\tbar width params are %lg, %lg, %lg\n",b_wid,b_int,b_spc);
  507. }
  508.  
  509. show_radius()
  510. {
  511.     fprintf(stderr,"\texplosion radius ratio is %lg\n",radexp);
  512. }
  513.  
  514. show_border()
  515. {
  516.     fprintf(stderr,"\tpicture frame is%s drawn\n",draw_border?"":" not");
  517. }
  518.  
  519. show_size()
  520. {
  521.     fprintf(stderr,"\tsize is scaled by %g, %g\n",xsize,ysize);
  522. }
  523.  
  524. show_title()
  525. {
  526.     if (!data_head.fname || !*(data_head.fname))
  527.         fprintf(stderr,"\tpicture has no title\n");
  528.     else
  529.         fprintf(stderr,"\ttitle for picture is: \"%s\"\n",data_head.fname);
  530. }
  531.  
  532. show_tresh()
  533. {
  534.     fprintf(stderr,"\tslices glue threshold is %lg with name \"%s\"\n", treshold, thrname);
  535. }
  536.  
  537. show_format()
  538. {
  539.     fprintf(stderr,"\tformat for numbers is: \"%s\"\n", tic_form);
  540. }
  541.  
  542. show_font()
  543. {
  544.     fprintf(stderr,"\tvector font is ");
  545.     switch (vect_font) {
  546.         case F_NEVER: fprintf(stderr,"never used\n"); break;
  547.         case F_WHENN: fprintf(stderr,"used when needed\n"); break;
  548.         case F_ROTAT: fprintf(stderr,"for rotated text\n"); break;
  549.         case F_ALWYS: fprintf(stderr,"always used\n"); break;
  550.     }
  551. }
  552.  
  553. show_trunc()
  554. {
  555.     fprintf(stderr,"\tlabels are%s truncated", strunc==-1 ? " not" : "");
  556.     if (strunc>0)
  557.         fprintf(stderr," at char no. %d", strunc);
  558.     fprintf(stderr,"\n");
  559. }
  560.